package cn.lili.modules.promotion.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.cglib.CglibUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.promotion.entity.dto.search.BoxSearchParams;
import cn.lili.modules.promotion.entity.vos.BoxOptionVO;
import cn.lili.modules.system.client.DictionaryClient;
import cn.lili.modules.system.constants.DictConstant;
import cn.lili.modules.system.entity.dos.Dictionary;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.lili.modules.promotion.entity.dos.BoxOption;
import cn.lili.modules.promotion.service.BoxOptionService;
import cn.lili.modules.promotion.mapper.BoxOptionMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author asus
* @description 针对表【li_box_option(购买方案表)】的数据库操作Service实现
* @createDate 2023-11-22 17:32:13
*/
@AllArgsConstructor
@Slf4j
@Service
public class BoxOptionServiceImpl extends ServiceImpl<BoxOptionMapper, BoxOption>
    implements BoxOptionService{

    private DictionaryClient dictionaryClient;

    @Override
    public ResultMessage<Object> pageBoxGoods(BoxSearchParams params) {
        Page<BoxOption> mhgoodsPage = super.page(new Page<>(params.getPageNumber(), params.getPageSize()), this.createLambdaQueryWrapper(params));
        return ResultUtil.data(this.conversion(mhgoodsPage));
    }

    private Page<BoxOptionVO> conversion(Page<BoxOption> paramsPage) {
        //复制page对象
        if (CollectionUtil.isEmpty(paramsPage.getRecords())) {
            return new Page<BoxOptionVO>();
        }
        Page<BoxOptionVO> resultPage = new Page<>();
        BeanUtils.copyProperties(paramsPage, resultPage);
        List<BoxOptionVO> voList = CglibUtil.copyList(paramsPage.getRecords(), BoxOptionVO::new);
        resultPage.setRecords(voList);

        //字典转换
        List<Dictionary> optionStatusList = dictionaryClient.getListByCode(DictConstant.OPTION_STATUS_NAME);
        for (BoxOptionVO boxOptionVO : resultPage.getRecords()) {
            if (CollectionUtil.isNotEmpty(optionStatusList)) {
                for (Dictionary dictionary : optionStatusList) {
                    if (dictionary.getValue().equals(String.valueOf(boxOptionVO.getOptionStatus()))) {
                        boxOptionVO.setOptionStatusName(dictionary.getValueName());
                    }
                }
            }
        }

        return resultPage;
    }

    @Override
    public ResultMessage<Object> addBoxGoods(BoxOption boxOption) {
        log.info("boxOptionVO == {}", JSONObject.toJSONString(boxOption));
        beforeCheck(boxOption);
        if (boxOption.getOptionType() == null) {
            throw new ServiceException("方案名为空");
        }
        boxOption.setId(null);
        if (CollectionUtil.isNotEmpty(boxOption.getGoodsList())) {
            boxOption.setBiZhongGoodsId(boxOption.getGoodsList().stream().map(Goods::getId).collect(Collectors.joining(",")));
        } else {
            boxOption.setBiZhongGoodsId(null);
        }
        boolean save = super.save(boxOption);
        return ResultUtil.data(save);
    }

    @Override
    public ResultMessage<Object> updateBoxGoods(BoxOptionVO boxOptionVO) {
        log.info("boxOptionVO == {}", JSONObject.toJSONString(boxOptionVO));
        beforeCheck(boxOptionVO);
        if (StringUtils.isEmpty(boxOptionVO.getId())) {
            throw new ServiceException("id为空");
        }
        if (CollectionUtil.isNotEmpty(boxOptionVO.getGoodsList())) {
            boxOptionVO.setBiZhongGoodsId(boxOptionVO.getGoodsList().stream().map(Goods::getId).collect(Collectors.joining(",")));
        }
        return ResultUtil.data(super.updateById(boxOptionVO));
    }

    /**
     * 构建搜索条件
     * @param params 参数
     * @return mybatis条件
     */
    private LambdaQueryWrapper<BoxOption> createLambdaQueryWrapper(BoxSearchParams params) {
        LambdaQueryWrapper<BoxOption> boxOptionLambdaQueryWrapper = new LambdaQueryWrapper<>();

        return boxOptionLambdaQueryWrapper;
    }


    /**
     * 参数校验
     * @param boxOption 参数
     */
    private void beforeCheck(BoxOption boxOption) {
        if (boxOption.getOptionType() == null || boxOption.getOptionType() < 0) {
            throw new ServiceException("购买方案异常");
        }
        if (boxOption.getOptionStatus() == null) {
            boxOption.setOptionStatus(DictConstant.OPTION_STATUS_01);
        }
//        if (boxOption.getOptionStatus()!= null && (boxOption.getOptionStatus() < 1 || boxOption.getOptionStatus() > 2)) {
//            throw new ServiceException("状态异常：1启用，2停用");
//        }
    }
}




